﻿// Learn more about F# at http://fsharp.net
module main

open System.IO
open System.Collections.Generic
open System.Collections


type node =
    {
        num_sub_node : int
        name : string
        node_list : list<node>
    }

let rec SeperateString(str : string)= 
 //   printfn "str = %s" str
    let mutable str_list = []
    if str.IndexOf(',') = -1
    then
        
        str_list <- str::[]
    else
        let mutable i = 0
        let mutable left_num = 0
        let mutable right_num = 0
        

        if str.IndexOf(',') < str.IndexOf('(') || str.IndexOf('(') = -1
        then
            let s = str.Substring(0, str.IndexOf(','))
            str_list <- str.Substring(0, str.IndexOf(',')) :: SeperateString(str.Substring(str.IndexOf(',') + 1, str.Length-str.IndexOf(',') - 1))
        else
            while i<str.Length
                do
                    if str.[i] = '('
                    then
                        left_num <- left_num + 1
                    if str.[i] = ')'
                    then    
                        right_num <- right_num + 1

                    if not(left_num = 0) && not(right_num = 0) && left_num = right_num
                    then
                        if i = str.Length - 1
                        then
                            str_list <- str::[]
                        else
                            let s = str.Substring(0, i + 1)
                            let s = str.Substring(i + 2, str.Length - i - 2)
                            str_list <- str.Substring(0, i + 1) :: SeperateString(str.Substring(i + 2, str.Length - i - 2))

                        i <- str.Length
                    else 
                        i<- i + 1
    str_list




let rec CreateTree(str : string) =

    if str.IndexOf('(') = -1
    then
        let leaf = {num_sub_node = 0; name = str; node_list = []}
        leaf
    else
        let l = SeperateString(str.Substring(str.IndexOf('(') + 1, str.Length - str.IndexOf('(') - 2))

        let mutable nodeList = []
        for item in l do
            nodeList <- CreateTree(item)::nodeList
        let reversed = List.rev nodeList
        let leaf = {
                        num_sub_node = l.Length;
                        name = str.Substring(0, str.IndexOf('('));
                        node_list = reversed
                    }
        leaf
    
let mutable num_tab = 0

let mutable case_str = ""
let mutable case_bool = false
let mutable sequence_type = ""

let rec translateTree (nod : node) = 
   
    match nod.name with
    |   "Behaviour" -> printfn "behaviour{"
                                              
                       num_tab <- num_tab + 1
                       for item in nod.node_list 
                            do 
                                let mutable i = 0
                                while i< num_tab
                                    do
                                    printf "  "
                                    i <- i + 1
                                translateTree(item)
                       num_tab <- num_tab - 1
                       printfn ";"
                       
                       let mutable i = 0
                       while i< num_tab
                            do
                                printf "  "
                                i <- i + 1 
                       printf "}"

    |   "Inert" ->  printf "done"

    |   "Unobservable" -> printf "//unobservable"

    |   "FirstThen" ->  translateTree(nod.node_list.[0])                        
                        printfn ";"
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1 
                        if nod.node_list.[0].name = "Project1"
                        then 
                            let n = nod.node_list.[0]
                            case_str <- n.node_list.[0].name
                            case_bool <- true
                        translateTree(nod.node_list.[1])

    |   "Define" -> translateTree(nod.node_list.[0])
                    printf " : "
                    translateTree(nod.node_list.[1])

    |   "Connection" -> printf "Connection["
                        for item in nod.node_list do translateTree(item)
                        printf "]"

    |   "Recv1" ->  let str1 = nod.node_list.[1].name.Replace("\"", "")
                    let str2 = nod.node_list.[2].name
                    printfn "%s : %s;" str1 str2
                    let mutable i = 0
                    while i< num_tab
                      do
                           printf "  "
                           i <- i + 1
                    let str1 = nod.node_list.[0].name.Replace("\"", "")
                    let str2 = nod.node_list.[1].name.Replace("\"", "")
                    printf "via %s receive %s" str1 str2

    |   "Recv2" ->  printf "via "
                    translateTree(nod.node_list.[0])
                    printf " receive "
                    translateTree(nod.node_list.[1])

    |   "Recv3" ->  printf "via "
                    translateTree(nod.node_list.[0])
                    printf " receive "
                    translateTree(nod.node_list.[1])
                    printf " : "
                    translateTree(nod.node_list.[2])

    |   "Recv" ->   printf "via "
                    translateTree(nod.node_list.[0])
                    printf " receive "
                    translateTree(nod.node_list.[1])

    |   "Send1" ->  let str1 = nod.node_list.[1].name.Replace("\"", "")
                    let str2 = nod.node_list.[2].name
                    printfn "%s : %s;" str1 str2
                    let mutable i = 0
                    while i< num_tab
                      do
                           printf "  "
                           i <- i + 1
                    let str1 = nod.node_list.[0].name.Replace("\"", "")
                    let str2 = nod.node_list.[1].name.Replace("\"", "")
                    printf "via %s send %s;" str1 str2

    |   "Send2" ->  printf "via "
                    translateTree(nod.node_list.[0])
                    printf " send "
                    translateTree(nod.node_list.[1])

    |   "Send3" ->  printf "via "
                    translateTree(nod.node_list.[0])
                    printf " send "
                    translateTree(nod.node_list.[1])
                    printf " : "
                    translateTree(nod.node_list.[2])

    |   "Send" ->   printf "via "
                    translateTree(nod.node_list.[0])
                    printf " send "
                    translateTree(nod.node_list.[1])
    
    |   "IfDo"  ->  printf "if ("
                    translateTree(nod.node_list.[0])
                    printfn ") do {"
                    let mutable j = 1
                    num_tab <- num_tab + 1
                    while j < nod.num_sub_node
                        do
                            let mutable i = 0
                            while i< num_tab
                                do
                                    printf "  "
                                    i <- i + 1
                            translateTree(nod.node_list.[j])
                            printfn ";"
                            j <- j+1
                    num_tab <- num_tab - 1
                    let mutable i = 0
                    while i< num_tab
                      do
                           printf "  "
                           i <- i + 1
                    printf "}"

    |   "IfThenElse" -> printf "if "
                        translateTree(nod.node_list.[0])
                        printfn ""

                        //then
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printfn "then{"
                        num_tab <- num_tab + 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        translateTree(nod.node_list.[1])
                        printfn ";"
                        num_tab <- num_tab - 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printfn "}"

                        //else
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printfn "else{"
                        num_tab <- num_tab + 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        translateTree(nod.node_list.[2])
                        printfn ";"
                        num_tab <- num_tab - 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printfn "}"

    |   "Equals"   
    |   "Equals1"   ->  translateTree(nod.node_list.[0])
                        printf " == "
                        translateTree(nod.node_list.[1])

    |   "NEquals1"   
    |   "NEquals"     ->  translateTree(nod.node_list.[0])
                          printf " <> "
                          translateTree(nod.node_list.[1])

    |   "Less1"
    |   "Less"     ->  translateTree(nod.node_list.[0])
                       printf " < "
                       translateTree(nod.node_list.[1])

    |   "More1"
    |   "More"     ->  translateTree(nod.node_list.[0])
                       printf " > "
                       translateTree(nod.node_list.[1])

    |   "EqualLess"
    |   "EqualLess1"     ->  translateTree(nod.node_list.[0])
                             printf " <= "
                             translateTree(nod.node_list.[1])

    |   "EqualMore"
    |   "EqualMore1"     ->  translateTree(nod.node_list.[0])
                             printf " <= "
                             translateTree(nod.node_list.[1])



    |   "Choose"    ->  printfn "choose {"
                        num_tab <- num_tab + 1

                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        translateTree(nod.node_list.[0])
                        num_tab <- num_tab - 1
                        printfn ""
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printfn "}"

    |   "Or" -> if case_bool
                then
                    translateTree(nod.node_list.[0])
                    printfn ""
                    let mutable i = 0
                    while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                    translateTree(nod.node_list.[1])

                else
                    translateTree(nod.node_list.[0])
                    printfn ";\n"
                    
                    let mutable i = 0
                    while i< num_tab
                        do
                            printf "  "
                            i <- i + 1

                    printfn "or"

                    let mutable i = 0
                    while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                    translateTree(nod.node_list.[1])
                    printf ";"

    |   "ReAssign" -> printf "replicate "
                      translateTree(nod.node_list.[0])
                      printf " = "
                      translateTree(nod.node_list.[1])

    |   "Assign" -> translateTree(nod.node_list.[0])
                    printf " = "
                    translateTree(nod.node_list.[1])

    |   "Assign2" -> translateTree(nod.node_list.[0])
                     printf " = "
                     translateTree(nod.node_list.[1])


    |   "Assign1"  ->  translateTree(nod.node_list.[0])
                       printf " : "
                       match nod.node_list.[1].name with
                       |    "New"   ->  let n = nod.node_list.[1]
                                        printf "%s" n.node_list.[0].name

                       |   "Connection_val" -> printf "Connection["
                                               translateTree(nod.node_list.[1].node_list.[0])
                                               printf "]"

                       |    "Int_val" -> printfn "Integer;"
                                         let mutable i = 0
                                         while i< num_tab
                                            do
                                                printf "  "
                                                i <- i + 1
                                         translateTree(nod.node_list.[0])
                                         printf " = "
                                         translateTree(nod.node_list.[1])

                       |    "Float_val" -> printfn "Float;"
                                           let mutable i = 0
                                           while i< num_tab
                                                do
                                                    printf "  "
                                                    i <- i + 1
                                           translateTree(nod.node_list.[0])
                                           printf " = "
                                           translateTree(nod.node_list.[1])

                       |    "String_val" -> printfn "String;"
                                            let mutable i = 0
                                            while i< num_tab
                                                do
                                                    printf "  "
                                                    i <- i + 1
                                            translateTree(nod.node_list.[0])
                                            printf " = "
                                            translateTree(nod.node_list.[1])

                       |    "Bool_val" -> printfn "Boolean;"
                                          let mutable i = 0
                                          while i< num_tab
                                               do
                                                   printf "  "
                                                   i <- i + 1
                                          translateTree(nod.node_list.[0])
                                          printf " = "
                                          translateTree(nod.node_list.[1])
                                 
                       |     _  ->  translateTree(nod.node_list.[1])

    |   "Compose"   ->  printfn "compose{"
                        num_tab <- num_tab + 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        translateTree(nod.node_list.[0])
                        num_tab <- num_tab - 1
                        printfn ";"
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printf "}"
                            
    |   "And"   ->  translateTree(nod.node_list.[0])
                    printfn ";"
                    let mutable i = 0
                    while i< num_tab
                        do
                           printf "  "
                           i <- i + 1
                    printfn "and"
                    let mutable i = 0
                    while i< num_tab
                        do
                           printf "  "
                           i <- i + 1
                    translateTree(nod.node_list.[1])
                    printf ";"

    |   "Free"  ->  printf "//free("
                    translateTree(nod.node_list.[0])
                    printf ")"

    |   "Repeat"    ->  printfn "replicate{"
                        num_tab <- num_tab + 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        translateTree(nod.node_list.[0])
                        num_tab <- num_tab - 1
                        printfn ";"
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1
                        printf "}"

    |   "NameBehav" ->  printf "value "
                        translateTree(nod.node_list.[0])
                        printfn " = behaviour{ "
                        num_tab <- num_tab + 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1 
                        translateTree(nod.node_list.[1])
                        printfn ";"
                        num_tab <- num_tab - 1
                        let mutable i = 0
                        while i< num_tab
                            do
                                printf "  "
                                i <- i + 1 
                        printf "}"

    |   "Unifies"   ->  translateTree(nod.node_list.[0])
                        printf " renames "
                        translateTree(nod.node_list.[1])

    |   "BehavWhere" -> translateTree(nod.node_list.[0])
                        translateTree(nod.node_list.[1])

    |   "Where" ->  printf "where{ "
                    translateTree(nod.node_list.[0])
                    printf " }"

    |   "Define1" -> translateTree(nod.node_list.[0])
                     printf " : "
                     translateTree(nod.node_list.[1])

    |   "DefineList" -> translateTree(nod.node_list.[0])
                        printf ", "
                        translateTree(nod.node_list.[1])

    |   "Abstrct1" -> printf "value "
                      translateTree(nod.node_list.[0])
                      printf " is abstraction("
                      //translateTree(nod.node_list.[1])
                      let n = nod.node_list.[1]
                      if n.name = "Define1"
                      then
                            translateTree(nod.node_list.[1])

                      else 
                            printf "tuple_value : tuple["

                            let rec translateDefineList (sub_node : node) =                      
                                match sub_node.name with
                                |   "DefineList" -> translateDefineList(sub_node.node_list.[0])
                                                    printf ", "
                                                    translateDefineList(sub_node.node_list.[1])                                    
                                |   "Define1" -> translateTree(sub_node.node_list.[1])
                                |   _ -> printf ""
 
                            translateDefineList(n.node_list.[0])
                            printf ", "
                            translateDefineList(n.node_list.[1])

                      printfn "){"
                      num_tab <- num_tab + 1
                      let mutable i = 0
                      while i< num_tab
                          do
                              printf "  "
                              i <- i + 1
                      translateTree(nod.node_list.[2])
                      num_tab <- num_tab - 1
                      printfn ";"
                      let mutable i = 0
                      while i< num_tab
                          do
                              printf "  "
                              i <- i + 1
                      printf "}"

    |   "Abstrct" ->  printf "value "
                      translateTree(nod.node_list.[0])
                      printfn " is abstraction(){"
                      num_tab <- num_tab + 1
                      let mutable i = 0
                      while i< num_tab
                          do
                              printf "  "
                              i <- i + 1
                      translateTree(nod.node_list.[1])
                      num_tab <- num_tab - 1
                      printfn ";"
                      let mutable i = 0
                      while i< num_tab
                          do
                              printf "  "
                              i <- i + 1
                      printf "}"

    
    |   "Decompose" -> printfn "//decompose"
                      
                       let n = nod.node_list.[0].node_list.[0].node_list.[0]
                       
                       let rec translateAnd (sub_node : node) =
                            
                            match sub_node.name with
                            |   "And" -> translateAnd(sub_node.node_list.[0])
                                         translateAnd(sub_node.node_list.[1])
                            |   _ -> let mutable i = 0
                                     while i< num_tab
                                        do
                                        printf "  "
                                        i <- i + 1
                                     printfn "{"
                                     num_tab <- num_tab + 1
                                     let mutable i = 0
                                     while i< num_tab
                                        do
                                        printf "  "
                                        i <- i + 1
                                     translateTree(sub_node)
                                     num_tab <- num_tab - 1
                                     printfn ";"
                                     let mutable i = 0
                                     while i< num_tab
                                        do
                                        printf "  "
                                        i <- i + 1
                                     printf "}"

                       translateAnd(n)

    |   "BehavPrecedences" -> translateTree(nod.node_list.[0])
                              translateTree(nod.node_list.[1])
                              
    |   "Precedences" -> printf " // always { "
                         translateTree(nod.node_list.[0])
                         printf " precedences "
                         translateTree(nod.node_list.[1])
                         printfn " }"
                              
    |   "Comma" -> translateTree(nod.node_list.[0])
                   printf " , "
                   translateTree(nod.node_list.[1])

    |   "SendVoid" -> printf "via "
                      translateTree(nod.node_list.[0])
                      printf " send 0"
    
    |   "RecvVoid" -> printf "via "
                      translateTree(nod.node_list.[0])
                      printf " receive 0"

    |   "NewBehav" ->  translateTree(nod.node_list.[0])
                       printf "("
                       printf ")"

    |   "NewBehav1" -> translateTree(nod.node_list.[0])
                       printf "("
                       translateTree(nod.node_list.[1])
                       printf ")"

    |   "NewBehav2" -> translateTree(nod.node_list.[0])
                       printf "("
                       translateTree(nod.node_list.[1])
                       printf ")"

    |   "Param" -> translateTree(nod.node_list.[0])
                   printf ","
                   translateTree(nod.node_list.[1])

    |   "ParamList" -> translateTree(nod.node_list.[0])
                       printf ", "
                       translateTree(nod.node_list.[1])

    |   "Recur" -> translateTree(nod.node_list.[0])

    |   "Tuple" -> printf "tuple["
                   translateTree(nod.node_list.[0])
                   printf "]"

    |   "Set" ->   printf "set["
                   translateTree(nod.node_list.[0])
                   printf "]"

    |   "Union" -> printf "union["
                   translateTree(nod.node_list.[0])
                   printf "]"

    |   "View" -> printf "view["
                  translateTree(nod.node_list.[0])
                  printf "]"

    |   "Variant" -> printf "variant["
                     translateTree(nod.node_list.[0])
                     printf "]"

    |   "Sequence" -> printf "sequence["
                      translateTree(nod.node_list.[0])
                      printf "]"

    |   "Tuple_val" -> printf "tuple("
                       translateTree(nod.node_list.[0])
                       printf ")"

    |   "View_val" -> printf "view("
                      translateTree(nod.node_list.[0])
                      printf ")"

    |   "Variant_val" -> printf "variant("
                         translateTree(nod.node_list.[0])
                         printf ")"

    |   "Seq_val" -> printf "sequence("
                     translateTree(nod.node_list.[0])
                     printf ")"
                     sequence_type <- nod.node_list.[0].node_list.[1].name

    |   "Union_val" -> printf "union("
                       translateTree(nod.node_list.[0])
                       printf ")"

    |   "Bool_val" -> translateTree(nod.node_list.[0])

    |   "String_val" -> printf "\""
                        translateTree(nod.node_list.[0])
                        printf "\""

    |   "Int_val" -> translateTree(nod.node_list.[0])

    |   "Any_val" -> printf "any("
                     translateTree(nod.node_list.[0])
                     printf ")"

    |   "Float_val" -> translateTree(nod.node_list.[0])

    |   "Id_val" -> translateTree(nod.node_list.[0])

    |   "Connection_val" -> printf "Connection("
                            translateTree(nod.node_list.[0])
                            printf ")"

    |   "ValList" -> let rec translateValList (sub_node : node) =                      
                            match sub_node.name with
                                |   "ValList" -> translateValList(sub_node.node_list.[0])
                                                 printf ","
                                                 translateValList(sub_node.node_list.[1])                                    
                                |   _ -> translateTree(sub_node)
                      
                     translateValList(nod.node_list.[0])
                     printf ","
                     translateValList(nod.node_list.[1])

    |   "TypeList" -> let rec translateTypeList (sub_node : node) =                      
                            match sub_node.name with
                                |   "TypeList" -> translateTypeList(sub_node.node_list.[0])
                                                  printf ", "
                                                  translateTypeList(sub_node.node_list.[1])                                    
                                |   _ -> translateTree(sub_node)
                      translateTypeList(nod.node_list.[0])
                      printf ","
                      translateTypeList(nod.node_list.[1])

    |   "TypeList1" -> translateTree(nod.node_list.[0])
                       printf ","
                       translateTree(nod.node_list.[1])

    |   "TypeList2" -> translateTree(nod.node_list.[0])
                       printf ","
                       translateTree(nod.node_list.[1])

    |   "TypeList3" -> translateTree(nod.node_list.[0])
                       printf ","
                       translateTree(nod.node_list.[1])

    |   "OperatorList" -> let rec translateOpList (sub_node : node) =                      
                            match sub_node.name with
                                |   "TypeList" -> translateOpList(sub_node.node_list.[0])
                                                  printf ", "
                                                  translateOpList(sub_node.node_list.[1])                                    
                                |   _ -> translateTree(sub_node)
                      
                          translateOpList(nod.node_list.[0])
                          printf ","
                          translateOpList(nod.node_list.[1])


    |   "Project1" -> printf "project "
                      translateTree(nod.node_list.[0])
                      printf " as "
                      translateTree(nod.node_list.[1])

    |   "Project" -> case_str <- nod.node_list.[1].name
                     translateTree(nod.node_list.[0])
                     printf " renames "
                     translateTree(nod.node_list.[1])

    |   "DefaultDo" -> printf "//case default: "
                       translateTree(nod.node_list.[0])
                       printf ";"

    |   "Case" -> printf "select %s{" case_str
                  printfn ""
                  let mutable i = 0
                  while i< num_tab
                    do
                        printf "  "
                        i <- i + 1
                  case_bool <- true
                  translateTree(nod.node_list.[0])
                  printfn ""
                  while i< num_tab
                    do
                        printf "  "
                        i <- i + 1
                  printfn "}"
                  case_bool <- false

                  

    |   "Do" -> printf "case "
                translateTree(nod.node_list.[0])
                printfn " do "
                num_tab <- num_tab + 1
                let mutable i = 0
                while i< num_tab
                    do
                        printf "  "
                        i <- i + 1
                translateTree(nod.node_list.[1])
                printf ";"
                num_tab <- num_tab - 1

    |   "Iterate1" -> printfn "index : Integer; "
                      let mutable i = 0
                      while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                      printfn " index = 0;"
                      let mutable i = 0
                      while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                      translateTree(nod.node_list.[1])
                      printf " : "
                      translateTree(nod.node_list.[2])
                      printfn ";"

                      let mutable i = 0
                      while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                      printf "while (index < "
                      translateTree(nod.node_list.[0])
                      printfn ".size){"

                      num_tab <- num_tab + 1
                      let mutable i = 0
                      while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                      translateTree(nod.node_list.[1])
                      printf " = "
                      translateTree(nod.node_list.[0])
                      printfn "(index);"
                 
                      let mutable i = 0
                      while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                      translateTree(nod.node_list.[3])
                      printfn ";"
                      num_tab <- num_tab - 1
                      let mutable i = 0
                      while i< num_tab
                        do
                            printf "  "
                            i <- i + 1
                      printf "}"

    |   "Alias"   -> printf "type "
                     translateTree(nod.node_list.[0])
                     printf " is "
                     translateTree(nod.node_list.[1])

    |   "Port" -> translateTree(nod.node_list.[1])

    |   "Connection_"
    |   "Connection__" -> printf "value "
                          translateTree(nod.node_list.[0])
                          printf " = connection("
                          translateTree(nod.node_list.[1])
                          printf ")"

    |   _   ->  let str = nod.name.Replace("\"", "")
                printf "%s" str

let rec travelTree (nod:node) = 
    let mutable i = 0
    while i< num_tab
        do 
            if i = num_tab - 1
            then    
                printf "|"
            printf "-"
            i <- i + 1
    printfn "%s" nod.name
    
    
    if not(nod.num_sub_node = 0)
    then
        num_tab <- num_tab + 1
        let mutable j = 0
        while j < nod.num_sub_node
            do
                travelTree nod.node_list.[j]
                j <- j+1 

        num_tab <- num_tab - 1

let print_string str = 
    printfn "%s" str

let print_newline =
    printfn ""

let printList l =
    List.iter print_string l
    print_newline

let strm = new StreamReader("..\\..\\..\\..\\PI_ADL_CC\\YaccLexing\\bin\\Debug\\outputfile.txt")
let str1 = strm.ReadToEnd()

let str2 = str1.Replace(" ", "")
let mutable str = str2.Replace("\n", "");

let mutable i = 0

while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("_val") + i
        if not (currentIndex = (-1 + i)) 
        then 
            if not(str.[currentIndex + 4]='(')
            then
                str <- str.Insert(currentIndex + 4, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index , ")");

            i <- currentIndex + 4;

        else
            i <- str.Length ;

i <- 0

while i < str.Length
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Quote") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 5] = '_')
        then
            if not(str.[currentIndex + 5]='(')
            then
                str <- str.Insert(currentIndex + 5, "(");
                let a = str.Substring(currentIndex + 7).IndexOf("\"");

                str <- str.Insert(currentIndex + 7 + a + 1, ")");
            i <- currentIndex + 7;

        else
            i <- i+1

           


i <- 0

while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("NewBehav\"") + i

        if not (currentIndex = (-1 + i))
        then 
            if not(str.[currentIndex + 8]='(')
            then
                str <- str.Insert(currentIndex + 8, "(");
                let a = str.Substring(currentIndex + 10).IndexOf("\"");

                str <- str.Insert(currentIndex + 10 + a + 1, ")");

            i <- currentIndex + 7;

        else
            i <- str.Length ;


i <- 0

while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("SendVoid") + i

        if not (currentIndex = (-1 + i))
        then 
            if not(str.[currentIndex + 8]='(')
            then
                str <- str.Insert(currentIndex + 8, "(");
                let a = str.Substring(currentIndex + 10).IndexOf("\"");

                str <- str.Insert(currentIndex + 10 + a + 1, ")");

            i <- currentIndex + 7;

        else
            i <- str.Length ;


i <- 0

while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("RecvVoid") + i

        if not (currentIndex = (-1 + i))
        then 
            if not(str.[currentIndex + 8]='(')
            then
                str <- str.Insert(currentIndex + 8, "(");
                let a = str.Substring(currentIndex + 10).IndexOf("\"");

                str <- str.Insert(currentIndex + 10 + a + 1, ")");

            i <- currentIndex + 7;

        else
            i <- str.Length ;

i <- 0

while i < str.Length - 9
    do 
        let currentIndex = str.[i..str.Length-1].IndexOf("Behaviour") + i
        if not (currentIndex = (-1 + i))
        then 
            if not(str.[currentIndex + 9]='(')
            then
                str <- str.Insert(currentIndex + 9, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");
                let c = str.Length - 1

                let mutable index = a;
                if not(a = -1) && not (b = -1)
                then
                    if a > b
                    then 
                        index <- b;
                elif not(a = -1) then index <- a
                elif not(b = -1) then index <- b
                elif a = -1 && b = -1 then index <- str.Length

                str <- str.Insert(currentIndex + index, ")");

            i <- currentIndex + 9;

        else
            i <- str.Length ;


i <- 0

while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Connection") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 10] = '_')
        then
            if not(str.[currentIndex + 10]='(')
            then
                str <- str.Insert(currentIndex + 10, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Location") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 8] = '_')
        then
            if not(str.[currentIndex + 8]='(')
            then
                str <- str.Insert(currentIndex + 8, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1

i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Sequence") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 8] = '_')
        then
            if not(str.[currentIndex + 8]='(')
            then
                str <- str.Insert(currentIndex + 8, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1



i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Tuple") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 5] = '_')
        then
            if not(str.[currentIndex + 5]='(')
            then
                str <- str.Insert(currentIndex + 5, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("View") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 4] = '_')
        then
            if not(str.[currentIndex + 4]='(')
            then
                str <- str.Insert(currentIndex + 4, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Union") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 5] = '_') 
        then
            if not(str.[currentIndex + 5]='(')
            then
                str <- str.Insert(currentIndex + 5, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Variant") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 7] = '_')
        then
            if not(str.[currentIndex + 7]='(')
            then
                str <- str.Insert(currentIndex + 7, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Set") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 3] = '_')
        then
            if not(str.[currentIndex + 3]='(')
            then
                str <- str.Insert(currentIndex + 3, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Bag") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex + 3] = '_')
        then
            if not(str.[currentIndex + 3]='(')
            then
                str <- str.Insert(currentIndex + 3, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1



i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("New") + i

        if currentIndex = -1 + i
        then
            i <- str.Length

        elif not(str.[currentIndex..currentIndex + 7] = "NewBehav")
        then
            if not(str.[currentIndex + 3]='(')
            then
                str <- str.Insert(currentIndex + 3, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");
            i <- currentIndex + 7;

        else
            i <- i+1


i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("Free") + i

        if not (currentIndex = (-1 + i))
        then 
            if not(str.[currentIndex + 4]='(')
            then
                str <- str.Insert(currentIndex + 4, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");

            i <- currentIndex + 5;

        else
            i <- str.Length ;

i <- 0
while i < str.Length - 6
    do 
        let currentIndex = str.[i..str.Length - 6].IndexOf("DefaultDo") + i

        if not (currentIndex = (-1 + i))
        then 
            if not(str.[currentIndex + 9]='(')
            then
                str <- str.Insert(currentIndex + 9, "(");
                let a = str.Substring(currentIndex).IndexOf(")");
                let b = str.Substring(currentIndex).IndexOf(",");

                let mutable index = a;
                if a > b
                then 
                    index <- b;

                str <- str.Insert(currentIndex + index, ")");

            i <- currentIndex + 5;

        else
            i <- str.Length ;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////create the tree///////////////////////////////////////////////////////////////////////

let tree = CreateTree(str);

printfn "Tree :"
printfn ""
travelTree tree

printfn ""
printfn ""
printfn ""
printfn "PI ADL .NET :"
printfn ""

translateTree tree
printfn ""
printfn ""
printfn ""


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////Print AST///////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
printfn "\nL'AST est : \n"
printfn "%s" str
printfn ""
printfn ""
printfn ""